var express = require("express");
var router = express.Router();
const productModel = require("../../models/productModel");

// 获取农产品列表
router.get("/product/:category", (req, res) => {
  let { category } = req.params;
  let { pageNo, pageSize } = req.query;
  if (pageNo === "undefined" || pageSize === "undefined") {
    productModel
      .find({category})
      .select({ _v: 0 })
      .then((data) => {
        if (data.length === 0) {
          return res.json({
            code: 200,
            msg: "数据为空",
            data: data,
          });
        }
        return res.json({
          code: 200,
          msg: "获取农产品列表成功",
          data: data,
        });
      })
      .catch((err) => {
        return res.json({
          code: "1001",
          msg: "数据库读取失败~~~",
          data: null,
        });
      });
  } else {
    productModel
      .find({category})
      .then((result) => {
        if (result.length === 0) {
          return res.json({
            code: 200,
            msg: "数据为空",
            data: null,
          });
        } else {
          productModel
            .find({category})
            .select({ _v: 0 })
            .skip((pageNo - 1) * pageSize)
            .limit(pageSize)
            .then((data) => {
              let total = result.length;
              return res.json({
                code: 200,
                msg: "获取农产品成功",
                data: data,
                total: total,
              });
            })
            .catch((err) => {
              return res.json({
                code: "1001",
                msg: "数据库读取失败~~~",
                data: null,
              });
            });
        }
      })
      .catch((err) => {
        return res.json({
          code: "1001",
          msg: "数据库读取失败~",
          data: null,
        });
      });
  }
});

// 查询
router.get("/product", (req, res) => {
  let { productName,category } = req.query;
  const regex = new RegExp(productName);
  productModel
    .find({ category,productName: regex })
    .select({ _v: 0 })
    .then((data) => {
      if (data.length === 0) {
        return res.json({
          code: 200,
          msg: "数据为空",
          data: data,
        });
      }
      return res.json({
        code: 200,
        msg: "查询成功",
        data: data,
        total:data.length
      });
    })
    .catch((err) => {
      return res.json({
        code: "1001",
        msg: "数据库读取失败~~~",
        data: null,
      });
    });
});

// 添加农产品
router.post("/product/add", (req, res) => {
  productModel.create({...req.body})
  .then((data) => {
    if (!data) {
      return res.json({
        code: -1,
        msg: "新增失败",
        data: null,
      });
    }
    return res.json({
      code: 200,
      msg: "新增成功",
      data: data,
    });
  })
  .catch((err) => {
    return res.json({
      code: "1001",
      msg: "数据库读取失败~~~",
      data: null,
    });
  });
})

// 修改农产品
router.post("/product/update", (req,res) => {
  let {_id} = req.body
  productModel.updateOne({ _id }, { ...req.body })
  .then((data) => {
    if (!data.acknowledged) {
      return res.json({
        code: -1,
        msg: "修改失败",
        data: null,
      });
    }
    return res.json({
      code: 200,
      msg: "修改成功~",
      data: null,
    });
  })
  .catch((err) => {
    return res.json({
      code: "1001",
      msg: "数据库读取失败~~~",
      data: null,
    });
  });
})

// 删除农产品
router.post("/product/remove", (req,res) => {
  let {_id} = req.body
  productModel.deleteOne({_id})
  .then((result) => {
    if (result.deletedCount !== 0) {
      return res.json({
        code: 200,
        msg: "删除成功",
        data: null,
      });
    }
    return res.json({
      code: -1,
      msg: "删除失败",
      data: null,
    });
  })
  .catch((err) => {
    return res.json({
      code: "1001",
      msg: "数据库读取失败~",
      data: null,
    });
  });
})

// 批量删除
router.post("/product/delBatch", (req,res) => {
  productModel
    .deleteMany({ _id: { $in: req.body } })
    .then((result) => {
      if (result.deletedCount !== 0) {
        return res.json({
          code: 200,
          msg: "删除成功",
          data: null,
        });
      }
      return res.json({
        code: -1,
        msg: "删除失败",
        data: null,
      });
    })
    .catch((err) => {
      return res.json({
        code: "1001",
        msg: "数据库读取失败~",
        data: null,
      });
    });
})

module.exports = router;
